﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Categorys
{
    internal class CategoryDAO
    {
        #region general

        private static Category LoadObj(IDataReader datareader)
        {
            Category obj = new Category();
            obj.CategoryID = MpDataHelp.GetInt(datareader, "CategoryID");
            obj.ParentID = MpDataHelp.GetInt(datareader, "ParentID");
            obj.Level = MpDataHelp.GetInt(datareader, "Level");
            obj.MenuID = MpDataHelp.GetString(datareader, "MenuID");
            obj.Target = MpDataHelp.GetString(datareader, "Target");
            obj.TypeCode = MpDataHelp.GetString(datareader, "TypeCode");
            obj.CategoryName = MpDataHelp.GetString(datareader, "CategoryName");
            obj.CategoryDESC = MpDataHelp.GetString(datareader, "CategoryDESC");
            obj.MetaTitle = MpDataHelp.GetString(datareader, "MetaTitle");
            obj.MetaKeyword = MpDataHelp.GetString(datareader, "MetaKeyword");
            obj.MetaDescription = MpDataHelp.GetString(datareader, "MetaDescription");
            obj.SeName = MpDataHelp.GetString(datareader, "SeName");
            obj.Icon = MpDataHelp.GetString(datareader, "Icon");
            obj.Link = MpDataHelp.GetString(datareader, "Link");
            obj.IsActive = MpDataHelp.GetBoolean(datareader, "IsActive");
            obj.IsUpdate = MpDataHelp.GetBoolean(datareader, "IsUpdate");
            obj.Default = MpDataHelp.GetBoolean(datareader, "Default");
            obj.SortOrder = MpDataHelp.GetInt(datareader, "SortOrder");
            obj.TotalPro = MpDataHelp.GetInt(datareader, "TotalPro");
            obj.DateCreate = MpDataHelp.GetDateTime(datareader, "DateCreate");

            return obj;
        }

        #endregion

        public static int Category_Add(Category obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Category_Add");
            db.AddOutParameter(dbCommand, "CategoryID", DbType.Int32, obj.CategoryID);
            db.AddInParameter(dbCommand, "ParentID", DbType.Int32, obj.ParentID);
            db.AddInParameter(dbCommand, "Level", DbType.Int32, obj.Level);
            db.AddInParameter(dbCommand, "MenuID", DbType.String, obj.MenuID);
            db.AddInParameter(dbCommand, "Target", DbType.String, obj.Target);
            db.AddInParameter(dbCommand, "TypeCode", DbType.String, obj.TypeCode);
            db.AddInParameter(dbCommand, "CategoryName", DbType.String, obj.CategoryName);
            db.AddInParameter(dbCommand, "CategoryDESC", DbType.String, obj.CategoryDESC);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            db.AddInParameter(dbCommand, "Icon", DbType.String, obj.Icon);
            db.AddInParameter(dbCommand, "Link", DbType.String, obj.Link);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            db.AddInParameter(dbCommand, "IsUpdate", DbType.Boolean, obj.IsUpdate);
            db.AddInParameter(dbCommand, "Default", DbType.Boolean, obj.Default);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
           
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@CategoryID"));
            return -1;
        }
     
        public static int Category_Update(Category obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Category_Update");
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, obj.CategoryID);
            db.AddInParameter(dbCommand, "ParentID", DbType.Int32, obj.ParentID);
            db.AddInParameter(dbCommand, "Level", DbType.Int32, obj.Level);
            db.AddInParameter(dbCommand, "MenuID", DbType.String, obj.MenuID);
            db.AddInParameter(dbCommand, "Target", DbType.String, obj.Target);
            db.AddInParameter(dbCommand, "TypeCode", DbType.String, obj.TypeCode);
            db.AddInParameter(dbCommand, "CategoryName", DbType.String, obj.CategoryName);
            db.AddInParameter(dbCommand, "CategoryDESC", DbType.String, obj.CategoryDESC);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            db.AddInParameter(dbCommand, "Icon", DbType.String, obj.Icon);
            db.AddInParameter(dbCommand, "Link", DbType.String, obj.Link);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            db.AddInParameter(dbCommand, "IsUpdate", DbType.Boolean, obj.IsUpdate);
            db.AddInParameter(dbCommand, "Default", DbType.Boolean, obj.Default);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);

            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Category_Delete(int categoryid)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Category_Delete");
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, categoryid);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Category> Category_Total(string TypeCode, int ParentID, int IsActive )
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Category_Total");
            db.AddInParameter(dbCommand, "ParentID", DbType.Int32, ParentID);
            db.AddInParameter(dbCommand, "IsActive", DbType.Int32, IsActive);
            db.AddInParameter(dbCommand, "TypeCode", DbType.String, TypeCode);
            List<Category> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Category>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }

        public static List<Category> Category_List(int categryID, int parentID, string SeName, string TypeCode, string MenuID, int IsActive, int SortOrder)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Category_List");
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, categryID);
            db.AddInParameter(dbCommand, "ParentID", DbType.Int32, parentID);
            db.AddInParameter(dbCommand, "SeName", DbType.String, SeName);
            db.AddInParameter(dbCommand, "TypeCode", DbType.String, TypeCode);
            db.AddInParameter(dbCommand, "MenuID", DbType.String, MenuID);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, IsActive);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, SortOrder);
            List<Category> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Category>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
   
    }
}
